<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>名称：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.name"
                        clearable
                        placeholder="名称"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>考试类型：</label>
                <div class="table-query-input">
                    <el-select v-model="queryData.examType" clearable>
                        <el-option label="职业题库" value="zhiye"></el-option>
                        <el-option label="高教题库" value="gjtk"></el-option>
                    </el-select>
                </div>
            </div>
            <div v-if="queryData.examType !== 'gjtk'" class="search-item">
                <label>行业：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.industryId"
                        v-loading="industryFlag"
                        filterable
                        clearable
                        placeholder="请选择行业"
                        reserve-keyword
                    >
                        <el-option
                            v-for="item in industryData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>考试：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.examId"
                        v-loading="examFlag"
                        filterable
                        clearable
                        placeholder="请选择考试"
                        reserve-keyword
                    >
                        <el-option
                            v-for="item in examData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>套餐：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.packageId"
                        v-loading="packageFlag"
                        filterable
                        clearable
                        placeholder="请选择套餐"
                        reserve-keyword
                    >
                        <el-option
                            v-for="item in packageData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>sku属性：</label>
                <el-cascader v-model="queryData.skuAttrIds" :options="skuAttrList" :show-all-levels="false"  :props="props"></el-cascader>
            </div>
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                type="danger"
                plain
                style="margin-bottom: 2em"
                icon="el-icon-delete"
                :disabled="selectDisable"
                @click="
                    tableDelete(
                        '/exam/package/sku/deletes',
                        {
                            ids: tableSelect
                        },
                        true
                    )
                "
            >批量删除
            </el-button>
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
                @selection-change="tableSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="industryName"
                    label="行业名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="examName"
                    label="考试名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="price"
                    label="原价￥"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        {{ priceChange(scope.row.price) }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="discountPrice"
                    label="折扣价￥"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        {{ priceChange(scope.row.discountPrice) }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="tmPrice"
                    label="天猫价￥"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        {{ priceChange(scope.row.tmPrice) }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="agentPrice"
                    label="代理价格￥"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        {{ priceChange(scope.row.agentPrice) }}
                    </template>
                </el-table-column>

                <el-table-column
                    prop="categoryNameList"
                    label="科目名称"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        <el-tag v-for="(item) in scope.row.categoryNameList" :key="item">{{ item }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="skuAttrNameList"
                    label="sku属性"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        <el-tag v-for="(item) in scope.row.skuAttrNameList" :key="item">{{ item }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="status"
                    label="状态"
                    min-width="60"
                    align="center"
                >
                    <template slot-scope="scope">
                        <k-tag :tag="scope.row.enable ? { desc:'已上架',color:'#6ADA8D'} : { desc:'已下架',color:'#fb4e4e'}"></k-tag>
                    </template>
                </el-table-column>

                <el-table-column
                    label="操作"
                    width="200"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="
                                clickSimpleEdit(
                                    scope.row,
                                    '编辑',
                                    () => {},
                                    coverData
                                )
                            "
                        >编辑
                        </el-button>
                        <el-button
                            v-if="scope.row.enable"
                            :loading="scope.row.opLoading"
                            type="text"
                            size="mini"
                            class="warning"
                            icon="el-icon-lock"
                            @click="lock(scope.row.id, false)"
                        >下架
                        </el-button>
                        <el-button
                            v-if="!scope.row.enable"
                            :loading="scope.row.opLoading"
                            size="mini"
                            type="text"
                            icon="el-icon-unlock"
                            @click="lock(scope.row.id, true)"
                        >上架
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="warning"
                            type="text"
                            icon="el-icon-delete"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/exam/package/sku/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true
                                )
                            "
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            width="70%"
            custom-class="two-dialog"
        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="6em"
                :rules="dataFormRule"
                class="table-edit-from"
            >
                <el-form-item label="产品名称" prop="name">
                    <el-input v-model="dataForm.name"></el-input>
                </el-form-item>
                <el-form-item label="所属套餐" prop="packageName">
                    <el-input v-model="dataForm.packageName" disabled></el-input>
                </el-form-item>
                <el-form-item label="所属考试" prop="examName">
                    <el-input v-model="dataForm.examName" disabled></el-input>
                </el-form-item>
                <el-form-item label="科目:" prop="categoryIds">
                    <el-cascader
                        v-model="dataForm.categoryIds"
                        v-loading="categoryFlag"
                        :options="categoryData"
                        :props="categoryProps"
                        clearable
                    ></el-cascader>
                </el-form-item>
                <el-form-item
                    label="产品"
                    prop="productIds"
                >

                    <el-select
                        v-model="dataForm.productIds"
                        multiple
                        placeholder="请选择产品"
                    >
                        <el-option
                            v-for="item in productData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="原价(元)" prop="price">
                    <el-input v-model="dataForm.price"></el-input>
                </el-form-item>
                <el-form-item label="折扣价(元)" prop="discountPrice">
                    <el-input v-model="dataForm.discountPrice"></el-input>
                </el-form-item>
                <el-form-item label="代理价(元)" prop="agentPrice">
                    <el-input v-model="dataForm.agentPrice"></el-input>
                </el-form-item>
                <el-form-item label="天猫价(元)" prop="tmPrice">
                    <el-input v-model="dataForm.tmPrice"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="dataForm.sort"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="save('/exam/package/sku/update')"
                >保 存</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {execCall, TablePage} from "@/layout/mixin/iVue.js";
import {mapState} from "vuex";
import {get, put} from "@/api/request";
import {changePackageSkuStatus} from "@/api/exam";
import {treeList2Tree} from "@/utils";

export default {
    name: "SkuList",
    components: {},
    mixins: [TablePage],
    data() {
        return {
            loadProductFirst:true,
            skuAttrFlag:false,
            skuAttrList:[],
            props: {
                multiple: true,
                checkStrictly:false,
                emitPath:false,
                label: "name",
                value:"id"
            },
            examType: null,
            industryData: [],
            examData: [],
            packageData: [],
            productData: [],
            categoryData: [],
            categoryFlag: false,
            productFlag: false,
            industryFlag: false,
            examFlag: false,
            packageFlag: false,
            queryData: {
                examType: null,
                base: true,
                name: "",
                industryId: null,
                examId: null,
                packageId: null,
            },
            categoryProps: {
                multiple: true,
                checkStrictly: true,
                emitPath: false,
                label: "name",
                value: "id",
            },
            initAddForm: {
                id: null,
                name: null,
                price: null,
                agentPrice: null,
                sort: 100,
                industryId: null,
                examId: null,
                categoryId: null,
                columnIds: [],
                days: null,
                endDate: null,
                examType: "zhiye"
            },
        };
    },
    computed: {
        ...mapState({})
    },
    watch: {
        "queryData.examType"() {
            if(this.queryData.examType === "gjtk"){
                this.queryData.industryId = "gjtk";
            }else {
                this.queryData.industryId = null;
            }
        },
        "queryData.industryId"(industryId) {
            this.queryData.examId = null;
            if (industryId) {
                this.loadExam(industryId);
            } else {
                this.examData = [];
            }
        },
        "queryData.examId"(examId) {
            this.queryData.packageId = null;
            if (examId) {
                this.loadPackage(examId);
            } else {
                this.packageData = [];
            }
        },
        "dataForm.examId"(examId) {
            if (examId) {
                this.loadCategory(examId);
            } else {
                this.categoryData = [];
            }

        },
        "dataForm.categoryIds"(categoryId) {
           this.loadProductData();

        },

    },
    created() {
        this.loadIndustry();
        this.loadSkuAttr();
    },
    mounted() {
    },
    methods: {
        search(queryData = {}) {
            this.loadTable("/exam/package/sku/page", {
                ...queryData,
                industryId:
                    this.queryData.examType === "gjtk"
                        ? "gjtk"
                        : this.queryData.industryId
            });
        },
        coverData(data) {
            let obj = {
                ...data
            }
            this.loadProductFirst = true;
            obj.price = obj.price / 100;
            obj.discountPrice = obj.discountPrice / 100;
            obj.agentPrice = obj.agentPrice / 100;
            obj.tmPrice = obj.tmPrice / 100;
            return obj;
        },
        loadCategory(examId, name) {
            this.categoryFlag = true;
            get("/exam/category/list", {
                examId,
                name,
                size: 1000,
                tree: false
            })
                .then(res => {
                    this.categoryData = treeList2Tree(res);
                })
                .finally(() => {
                    this.categoryFlag = false;
                });
        },
        loadProductData() {
            if (!this.loadProductFirst){
                this.dataForm.productIds = null;
                this.productData = [];
            }
            this.loadProductFirst = false;
            if (this.dataForm.categoryIds) {
                this.productFlag = true;
                get("/exam/product/base/listByCategoryIds", {
                    categoryIds: this.dataForm.categoryIds,
                    size: 1000,
                })
                    .then(res => {
                        this.productData = res;
                    })
                    .finally(() => {
                        this.productFlag = false;
                    });
            }

        },
        lock(id, status) {
            changePackageSkuStatus({id: id, enable: status}).then(res => {
                this.$message.success("修改成功");
                this.search();
            })
        },
        save(url){
            this.dataForm.price =  this.dataForm.price * 100;
            this.dataForm.discountPrice =  this.dataForm.discountPrice * 100;
            this.dataForm.agentPrice =  this.dataForm.agentPrice * 100;
            this.dataForm.tmPrice =  this.dataForm.tmPrice * 100;
            put(url,this.dataForm).then( res =>{
                this.dialogFormVisible = false;
                this.search();
            }).finally( () =>{
                this.submitFlag = false;
            })
        },
        loadIndustry() {
            this.industryFlag = true;
            get("/exam/industry/list",{size: 1000})
                .then(res => {
                    this.industryData = res;
                })
                .finally(() => {
                    this.industryFlag = false;
                });
        },loadSkuAttr() {
            this.skuAttrFlag = true;
            get("/exam/package/sku/attr/list",{size:1000,enable:true})
                .then(res => {
                    this.skuAttrList = treeList2Tree(res);
                })
                .finally(() => {
                    this.skuAttrFlag = false;
                });
        },

        loadExam(industryId, name) {
            this.examFlag = true;
            get("/exam/exam/list", {
                industryId,
                name,
                size: 1000
            })
                .then(res => {
                    this.examData = res;
                })
                .finally(() => {
                    this.examFlag = false;
                });
        },
        loadPackage(examId, name) {
            this.packageFlag = true;
            get("/exam/package/list", {
                examId,
                name,
                size: 1000
            })
                .then(res => {
                    this.packageData = res;
                })
                .finally(() => {
                    this.packageFlag = false;
                });
        },
        priceChange(number) {
            const bs = number / 100;
            return bs.toFixed(2) + "元";
        },
    }
};
</script>

<style lang="scss" scoped>
::v-deep .el-transfer-panel {
    width: 300px;
}
</style>
